/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.api.entity.RoleApi;
import com.ocm.system.service.IRoleApiService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.api.bo.ApiBO;
import com.ocm.api.dto.ApiDTO;
import com.ocm.api.entity.Api;
import com.ocm.api.vo.ApiVO;
import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.node.INode;
import com.ocm.core.tool.utils.BeanUtil;
import com.ocm.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.ocm.system.wrapper.ApiWrapper;
import com.ocm.system.service.IApiService;
import com.ocm.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author HQY
 * @since 2021-11-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api")
@io.swagger.annotations.Api(value = "", tags = "接口")
public class ApiController extends BladeController {

	private final IApiService apiService;

	private final IRoleApiService roleApiService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入api")
	public R<ApiVO> detail(Api api) {
		Api detail = apiService.getOne(Condition.getQueryWrapper(api));
		return R.data(ApiWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树形列表", notes = "传入api")
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> api) {
		List<Api> list = apiService.list(Condition.getQueryWrapper(api, Api.class).lambda().orderByAsc(Api::getSort));
		return R.data(ApiWrapper.build().listNodeVO(list));
	}

	//获取所有开启状态及有效的api接口信息 用于网关鉴权
	@GetMapping("/getAll")
	public List<ApiBO> getAllApi() {
		List<ApiBO> apiBOS=new ArrayList<>();
		List<ApiDTO> apiDTOS=apiService.selectApiList(new QueryWrapper<ApiDTO>()
			.eq("a.is_deleted",0)
			.eq("a.status",1)
			.groupBy("a.id")
			.orderByAsc("sort"));
		apiDTOS.forEach(apiDTO ->{
				ApiBO apiBO = new ApiBO(apiDTO.getRoles()==null?null:Arrays.asList(apiDTO.getRoles().split(",")));
				BeanUtil.copy(apiDTO,apiBO);
				apiBOS.add(apiBO);
			}

		);
		return apiBOS;
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入api")
	public R<IPage<ApiVO>> page(ApiVO api, Query query) {
		IPage<ApiVO> pages = apiService.selectApiPage(Condition.getPage(query), api);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入api")
	public R save(@Valid @RequestBody Api api) {
		return R.status(apiService.save(api));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入api")
	public R update(@Valid @RequestBody Api api) {
		return R.status(apiService.updateById(api));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入api")
	public R submit(@Valid @RequestBody Api api) {
		return R.status(apiService.saveOrUpdate(api));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Transactional
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

		List<Long> longs = Func.toLongList(ids);
		roleApiService.remove(new QueryWrapper<RoleApi>().in("api_id",longs));
		return R.status(apiService.deleteLogic(longs));
	}


}
